﻿using Base.Runtime;
using UnityEngine;
using UnityEngine.EventSystems;
using Extension.Runtime;

namespace UIModule
{
    public enum ELayer
    {
        Default,
    }
    
    public class UIWorld: XBehaviourObject
    {
        public Transform Root { get; private set; }
        public Canvas UIRoot { get; private set; }
        public Camera UICamera { get; private set; }
        public EventSystem UIEventSystem { get; private set; }

        public UIStackLayer Default{ get; private set; }
        
        protected override void OnInit()
        {
            Root = UtilityHierarchy.GetOrCreateByHierarchyPath("World", "UIWorld").transform;
            UIRoot = UtilityHierarchy.GetOrCreateByParentPath<Canvas>( Root,"UIRoot");
            UICamera = UtilityHierarchy.GetOrCreateByParentPath<Camera>(Root,"UICamera");
            UIEventSystem = UtilityHierarchy.GetOrCreateByParentPath<EventSystem>(Root,"EventSystem");
            Default = new UIStackLayer("Default", UIRoot);
            Default.Init();
        }

        protected override void OnUpdate()
        {
            Default.Update();
        }
        
        protected override void OnDispose()
        {
            Default.Dispose();
            ReleaseFields();
        }

        private void ReleaseFields()
        {
            Default = null;
            Root = null;
            UIRoot = null;
            UICamera = null;
            UIEventSystem = null;
        }

        public void OpenWindow()
        {
            
        }

        public static T Instantiate<T>(T prefab) where T : UnityEngine.Object
        {
            return Object.Instantiate(prefab);
        }
    }
}